//
//  WBLayoutView.swift
//  SinaWeibo
//
//  Created by zhuangqianwei on 16/11/12.
//  Copyright © 2016年 zhuangqianwei. All rights reserved.
//

import UIKit

class WBLayoutView: UITabBar {
     //定义一个闭包
    var composeClosure: (() -> ())?
    //重新添加一个tabbar的标签
    override init(frame: CGRect) {
        
        super.init(frame: frame)
       //tabbar 添加按钮
        self.addSubview(composeBtn)
      //按钮的监听事件
        composeBtn.addTarget(self, action: #selector(ClickBtn), for: .touchUpInside)
        
    }
    
   @objc private func ClickBtn() {
        
        //执行按钮的点击方法
        //需要跳转控制器，不能在view里面处理点击事件
        
        composeClosure?()
        
    
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
    
        //只要重写了构造方法，就必须实现该方法
        //只能手写代码和xib 创建
        super.init(coder: aDecoder)
        self.addSubview(composeBtn)
        
    }
    
    
  
    //自定义tabbar的布局
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        let w = UIScreen.main.bounds.width / 5
        let h = self.bounds.height
        var index = 0
        
        //遍历tabbar
        for subview in subviews {
            
      //判断类型
            if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                subview.frame = CGRect(x: CGFloat(index) * w, y: 0, width: w, height: h)
            //下标递增
            if index == 1 {
                index += 1
            }
            index += 1
            
            
            
            }
            
        }
        //撰写按钮的布局
        composeBtn.frame.size = CGSize(width: w, height: h)
        composeBtn.center = CGPoint(x: self.center.x, y:h * 0.5 )
        
    }
    
    //懒加载按钮
    lazy var composeBtn: UIButton = {
    let btn = UIButton()
    //设置按钮的图片
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        //设置背景图片
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
           return btn
    }()
}
